/**
 * Created by 86185 in 2020/8/2 on 10:28
 */
class ObjectUtil {
  constructor() {
  }

  /**
   * 检查对象是否为空
   * @param obj
   * @returns {boolean}
   */
  isEmpty(obj) {
    return Reflect.ownKeys(obj).length === 0 && obj.constructor === Object;
  }

  /**
   * 剔除对象中指定的属性
   * @param object
   * @param props
   * @returns {{}}
   */
  omit(object, props = []) {
    let res = {};
    Object.keys(object).forEach(key => {
      if (!props.includes(key)) {
        res[key] = typeof object[key] === "object" && object[key] !== null ?
            JSON.parse(JSON.stringify(object[key])) :
            object[key]
      }
    })
    return res
  }

  /**
   * 深拷贝一个对象
   * @param obj
   * @param hash
   * @returns {any|RegExp|Date}
   */
  deepCopy(obj, hash = new WeakMap()) {
    if (obj instanceof Date) {
      return new Date(obj);
    }
    if (obj instanceof RegExp) {
      return new RegExp(obj);
    }
    if (hash.has(obj)) {
      return hash.get(obj);
    }
    let allDesc = Object.getOwnPropertyDescriptors(obj);
    let cloneObj = Object.create(Object.getPrototypeOf(obj), allDesc);
    hash.set(obj, cloneObj);
    for (let key of Reflect.ownKeys(obj)) {
      if (obj[key] && typeof obj[key] === "object") {
        cloneObj[key] = deepCopy(obj[key], hash);
      } else {
        cloneObj[key] = obj[key];
      }
    }
    return cloneObj;
  }
}

export default new ObjectUtil()
